// pages/homelink/zqy_shoudai/zqy_shoudai.js
import gcoord from '../../../../miniprogram_npm/gcoord/dist/gcoord'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    distance: "1.0",
    longitude: "",
    latitude: "",
    pageNum: 1,
    data: []
  },

  getlistgengduo () {
    var _this = this
    wx.showLoading({
      title: '加载中',
    })
    const params = {
      distance: _this.data.distance,
      userLng: _this.data.longitude,
      userLat: _this.data.latitude,
      firstCredit: "1",
      pageBO: {
        pageSize: 50,
        pageNum: _this.data.pageNum
      }
    }
    getApp().globalData.request('/map/lookAround', params, "POST",).then(res => {
      setTimeout(function () {
        wx.hideLoading()
      }, 500)
      var rstObj = res
      console.log(res);
      for (let p = 0; p < rstObj.list.length; p++) {
        var resae = gcoord.transform(
          [rstObj.list[p].longitude, rstObj.list[p].latitude],
          gcoord.BD09, // 当前坐标系
          gcoord.GCJ02, // 目标坐标系
        );
        rstObj.list[p].longitude = resae[0]
        rstObj.list[p].latitude = resae[1]
      }

      _this.setData({
        data: _this.data.data.concat(res.list),
      })

    })
  },
  clcik (e) {
    console.log(e.currentTarget.id);
    wx.navigateTo({
      url: '/pages/Company/Company?corpKey=' + e.currentTarget.id,
    })
  },
  phone (event) {
    wx.makePhoneCall({
      phoneNumber: event.currentTarget.id
    })
  },
  location (event) {
    var _this = this
    wx.openLocation({
      latitude: _this.data.data[event.currentTarget.id].latitude,
      longitude: _this.data.data[event.currentTarget.id].longitude,
      name: _this.data.data[event.currentTarget.id].d2CorpName,
      address: _this.data.data[event.currentTarget.id].d2Dom,
      scale: 18
    })
  },
  // 滑动底部加载更多
  scrollBottom () {
    this.onBottom();
  },
  onBottom () {
    (this.data.pageNum)++;
    this.getlistgengduo();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    var _this = this
    getApp().globalData.getLocation({
      type: "gcj02",
      success (res) {
        console.log(res)
        _this.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        })
        var resae = gcoord.transform(
          [res.longitude, res.latitude],
          gcoord.GCJ02, // 目标坐标系
          gcoord.BD09, // 当前坐标系
        );
        // console.log(resae);
        _this.setData({
          longitude: resae[0],
          latitude: resae[1],
        })

        wx.showLoading({
          title: '加载中',
        })
        const params = {
          distance: _this.data.distance,
          userLng: _this.data.longitude,
          userLat: _this.data.latitude,
          firstCredit: "1",
          pageBO: {
            pageSize: 50,
            pageNum: 1
          }
        }

        getApp().globalData.request('/map/lookAround', params, "POST",).then(res => {
          setTimeout(function () {
            wx.hideLoading()
          }, 500)
          var rstObj = res
          for (let p = 0; p < rstObj.list.length; p++) {

            var resae = gcoord.transform(
              [rstObj.list[p].longitude, rstObj.list[p].latitude],
              gcoord.BD09, // 当前坐标系
              gcoord.GCJ02, // 目标坐标系
            );
            rstObj.list[p].longitude = resae[0]
            rstObj.list[p].latitude = resae[1]
          }

          _this.setData({
            data: rstObj.list,
          })
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage () {

  }
})